//
//  TFindPassengerListViewController.m
//  LoveShare
//
//  Created by ways on 2017/4/27.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "TFindPassengerListViewController.h"

#import "TReleaseFindPassengerViewController.h"
#import "TFindPassengerListTableView.h"

#import "TPassengerViewModel.h"

@interface TFindPassengerListViewController ()

@property (strong, nonatomic) TPassengerViewModel *passengerViewModel;
@property (strong, nonatomic) IBOutlet TFindPassengerListTableView *tableView;
@property (strong, nonatomic) IBOutlet UIButton *releaseButton;

@end

@implementation TFindPassengerListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initProperty];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

#pragma mark - set

- (void)setIsReleaseFindDriverComing:(BOOL)isReleaseFindDriverComing {
    _isReleaseFindDriverComing = isReleaseFindDriverComing;
    
}


- (TPassengerViewModel *)passengerViewModel {
    if (!_passengerViewModel) {
        _passengerViewModel = [[TPassengerViewModel alloc] initPassengerViewModel];
        _passengerViewModel.requestURL = T_GetFindCarAllInfo;
    }
    return _passengerViewModel;
}

#pragma mark - init

- (void)initProperty {
    
    [self initNavigation];
    
    if (_isReleaseFindDriverComing) {
        self.title = @"找司机发布记录";
        [self.releaseButton setTitle:@"我要找司机" forState:UIControlStateNormal];
    } else self.title = @"找乘客";
    
    @weakify(self)
    [[self.releaseButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.isReleaseFindDriverComing) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            TReleaseFindPassengerViewController *passengerVC = [[TReleaseFindPassengerViewController alloc] init];
            passengerVC.releaseSuccessedBlock = ^{
                [self.tableView.mj_header beginRefreshing];
            };
            [self.navigationController pushViewController:passengerVC animated:YES];
        }
        
    }];
    
    [self initMJRefresh];
}

- (void)initNavigation {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:@"common_back_barItem_button"] forState:UIControlStateNormal];
    //            @weakify(self);
    TWeakSelf(self)
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        //                @strongify(self);
        [weakself.view endEditing:YES];
        if (weakself.isReleaseFindDriverSuccessedComing) {
            [weakself.navigationController popToRootViewControllerAnimated:YES];
        } else {
            [weakself.navigationController popViewControllerAnimated:YES];
        }
        
    }];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)initMJRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //Call this Block When enter the refresh status automatically
        [self loadNewData];
    }];
    // Enter the refresh status immediately
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //Call this Block When enter the refresh status automatically
        [self loadMoreData];
    }];
    self.tableView.mj_footer.automaticallyHidden = YES;
}


#pragma mark -

- (void)loadNewData {
    NSInteger currentPage = [self.passengerViewModel.findPassengerListModel.page integerValue];
    self.passengerViewModel.findPassengerListModel.page = @"1";//只要是下拉刷新重新请求第一页数据
    @weakify(self)
    [[self.passengerViewModel.requestCommand execute:nil] subscribeNext:^(TFindPassengerListModel  *_Nullable findPassengerListModel) {
        @strongify(self)
        if (findPassengerListModel == nil) {
            self.passengerViewModel.findPassengerListModel.page = [@(currentPage) stringValue];
        } else {
            [self.tableView.mj_footer resetNoMoreData];
            NSMutableArray *findPassengerListModels = [TKCAppTools changeArray:[NSMutableArray arrayWithArray:findPassengerListModel.result.data] orderWithKey:@"goTime" ascending:YES];
            self.tableView.findPassengerListModels = findPassengerListModels;
            [self.tableView.mj_header endRefreshing];
            if (([findPassengerListModel.result.total integerValue] == self.tableView.findPassengerListModels.count)) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
    }];
}

- (void)loadMoreData {
    NSInteger currentPage = [self.passengerViewModel.findPassengerListModel.page integerValue];
    self.passengerViewModel.findPassengerListModel.page = [@"" stringByAppendingString:[@(++currentPage) stringValue]];
    //    [self.tableView.mj_footer performSelector:@selector(endRefreshing) withObject:nil afterDelay:1.0];
    @weakify(self)
    [[self.passengerViewModel.requestCommand execute:nil] subscribeNext:^(TFindPassengerListModel  *_Nullable findPassengerListModel) {
        @strongify(self)
        if (findPassengerListModel == nil) {
            self.passengerViewModel.findPassengerListModel.page = [@(currentPage) stringValue];
        } else {
            //            self.tableView.findPassengerListModels = findPassengerListModel.result;
            if (findPassengerListModel.result.data.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer endRefreshing];
                NSMutableArray *findPassengerListModels = [TKCAppTools changeArray:[NSMutableArray arrayWithArray:findPassengerListModel.result.data] orderWithKey:@"goTime" ascending:YES];
                [self.tableView.findPassengerListModels addObjectsFromArray:findPassengerListModels];
                [self.tableView reloadData];
                if (([findPassengerListModel.result.total integerValue] == self.tableView.findPassengerListModels.count)) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
        }
    }];
}

@end
